package com.cdh.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdh.oa.entity.User;
import com.cdh.oa.service.IMenuService;
import com.cdh.oa.service.IUserService;
import com.cdh.oa.util.ConstUtil;
import com.cdh.oa.util.MD5Util;
import com.cdh.oa.util.ResponseData;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;

@Controller
@Slf4j
public class LoginController {

    @Autowired
    private DefaultKaptcha kaptcha;

    @Autowired
    private IUserService userService;

    @Autowired
    private IMenuService menuService;

    @GetMapping("/kaptchaImage")
    public void getKaptchaImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成随机字符
        String code = kaptcha.createText();
        //把生成的随机字符存储到session中，便于后面验证码判断
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY,code);
        log.debug("验证码："+code);
        //通过验证码字符串生成配置好的图片
        BufferedImage image = kaptcha.createImage(code);
        //把图片通过流的形式返回到客服端
        // 1.设置返回头信息
        response.setHeader(HttpHeaders.PRAGMA,"no-cache");
        response.setHeader(HttpHeaders.CACHE_CONTROL,"no-cache");
        response.setHeader(HttpHeaders.EXPIRES,"0");

        ImageIO.write(image,"JPEG",response.getOutputStream());
    }

    @GetMapping({"/"})
    public String loginPage(){
        return "login";
    }

    @ResponseBody
    @PostMapping("/login")
    public ResponseData login(String logName , String logPwd, String kaptcha, HttpSession session){
        String sessionKaptcha = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        //TODO 验证码验证
       /* if(!kaptcha.equalsIgnoreCase(sessionKaptcha)){
            return ResponseData.fail("验证码输入有误！");
        }*/
        //根据用户名和密码查询是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",logName).and(qw->qw.eq("pwd", MD5Util.getMD5(logPwd)));
        User user = userService.getOne(queryWrapper);
        if(Objects.isNull(user)){
            return ResponseData.fail("用户名或密码输入有误！！");
        }
        if(user.getStatus() == ConstUtil.STATUS_DISABLED){
            return ResponseData.fail("您已被禁用，请联系管理员！！");
        }
        //把登录用户的密码去除，并添加到Session中
        user.setPwd(null);
        session.setAttribute(ConstUtil.CURRENT_USER,user);
        return ResponseData.okMsg("登录成功！！");
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @ResponseBody
    @GetMapping("/menu")
    public ResponseData menu(){
        return ResponseData.okData(menuService.navList());
    }

}
